#include "stm32f10x.h"
#include "systick.h"

uint32_t ms_value, us_value;

void Delay_init(uint8_t SYSCLK)
{
    SysTick->CTRL = 0xfffffff8;
    us_value = SYSCLK / 8;
    ms_value = us_value * 1000;
}
void Delay_us(uint32_t nus)
{
    uint32_t temp;
    SysTick->LOAD = (uint32_t)nus * us_value;
    SysTick->VAL = 0x00;
    SysTick->CTRL |= 0x01;
    do
    {
        temp = SysTick->CTRL;
    }
    while(temp & 0x01 && !(temp & (1 << 16)));
    SysTick->CTRL &= 0xfffffff8;
    SysTick->VAL = 0x00;
}
void Delay_ms(uint32_t nms)
{
    uint32_t temp;
    SysTick->LOAD = (uint32_t)nms * ms_value;
    SysTick->VAL = 0x00;
    SysTick->CTRL |= 0x01;
    SysTick->CTRL |= 0x01;
    do
    {
        temp = SysTick->CTRL;
    }
    while(temp & 0x01 && !(temp & (1 << 16)));
    SysTick->CTRL &= 0xfffffff8;
    SysTick->VAL = 0x00;
}
